const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async (event, context) => {
  const db = cloud.database()
  // 1. 解构参数
  const {
    userId,
    nickName,
    real_name,
    age,
    gender,
    phone,
    address,
    hobby,
    createdTime,
    isDeleted
  } = event

  try {
    // 1. 执行更新
    const result = await db.collection('users').doc(userId).update({
      data: {
        nickName,
        real_name,
        age,
        gender,
        phone,
        address,
        hobby,
        createdTime,
        isDeleted,
        editTime: cloud.database().serverDate(),
      }
    })

    // 2. 返回成功响应
    return {
      code: 200,
      message: '更新成功',
      data: result
    }
  } catch (err) {
    console.error(err)
    return {
      code: 500,
      message: '服务器错误'
    }
  }
}